package com.inspector.app.di.location

import android.Manifest
import android.app.Notification
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import androidx.core.app.ActivityCompat
import com.inspector.app.util.CommonUtil
import com.inspector.app.util.LogUtil
import java.lang.ref.WeakReference


/**
 * 定位
 */
open class LocationClient internal constructor(builder: Builder) {

    constructor() : this(Builder())

    private val logger = "LocationClient"
    private var contextRef: WeakReference<Context>? = builder.contextRef
    private var notification: Notification? = builder.notification
    private var locationListener: CustomLocationListener? = builder.locationListener
    private var minInterval: Long = builder.minInterval
    private var minDistance: Float = builder.minDistance
    private var coordinateType: CustomCoordinateType = builder.coordinateType   //[默认GPS 即w84坐标系]
    private var locationType: CustomLocationType = builder.locationType            //[当前定位类型]
    val mapLocationType get() = locationType

    class Builder constructor() {
        internal var contextRef: WeakReference<Context>? = null
        internal var notification: Notification? = null
        internal var locationListener: CustomLocationListener? = null

        internal var minInterval: Long = 0L  //[定位间隔时间, 单位毫秒]
        internal var minDistance: Float = 0f //[定位间隔距离, 单位米]

        internal var coordinateType: CustomCoordinateType = CustomCoordinateType.GPS //[默认GPS 即w84坐标系]
        internal var locationType: CustomLocationType = CustomLocationType.LOCATION_DEFAULT //[当前定位类型]


        internal constructor(downloadClient: LocationClient) : this() {
            this.notification = downloadClient.notification
        }

        fun context(applicationContext: Context) = apply {
            this.contextRef = WeakReference(applicationContext)
        }

        //[添加通知,华为鸿蒙定位可能会用到]
        fun addNotification(notification: Notification) = apply {
            this.notification = notification
        }

        //[重写定位回调方法, onLocationChanged]
        fun setLocationListener(locationListener: CustomLocationListener) = apply {
            this.locationListener = locationListener
        }

        //https://developer.android.com/reference/android/location/LocationManager#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)
        fun minInterval(minInterval: Long) = apply {
            this.minInterval = minInterval
        }
        fun minDistance(minDistance: Float) = apply {
            this.minDistance = minDistance
        }

        //[当前定位类型, 默认类型(华为机型上用鸿蒙定位, 非华为机型用安卓原生定位)]
        fun setLocationType(type: CustomLocationType) = apply {
            this.locationType = type
        }

        fun build(): LocationClient = LocationClient(this)

    }

    open fun newBuilder(): Builder = Builder(this)

    //[检查定位和模糊定位权限(Android12 https://lbs.amap.com/api/android-location-sdk/guide/utilities/permision_12)]
    private fun checkSelfPermission(context: Context): Boolean {
        return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
    }


    private var locationManager: LocationManager? = null
    fun startLocation() {
        val context = contextRef?.get() ?: return
        if (checkSelfPermission(context)) {  // 定位权限问题,目前会在打开app登录的时候检查
            LogUtil.e(logger, "无定位权限")
            return
        }
        locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

        when (locationType) {
            CustomLocationType.LOCATION_GD -> {
                LogUtil.e(logger, "高德地图定位模式")
                GDLocationInstance.getInstance().startLocation(context, minInterval, locationListener)
            }

            CustomLocationType.LOCATION_MULTIPLE -> {
                LogUtil.e(logger, "原生或华为鸿蒙定位为主 高德定位辅助定位")
                if (CommonUtil.isHarmonyOs()) { //[华为HMS服务-避免鸿蒙系统安卓定位问题]]
                    HWLocationInstance.getInstance().startLocation(context, notification, minInterval, locationListener)
                } else GPSLocationInstance.instance.startLocation(locationManager, minInterval, minDistance, locationListener)
                //[补点定位时间间隔暂写死1000L]
                GDLocationInstance.getInstance().startLocation(context, 0L, locationListener)
            }

            else -> { // MapLocationType.LOCATION_DEFAULT
                LogUtil.e(logger, "默认定位模式")
                if (CommonUtil.isHarmonyOs()) { //[华为HMS服务-避免鸿蒙系统安卓定位问题]]
                    HWLocationInstance.getInstance().startLocation(context, notification, minInterval, locationListener)
                } else GPSLocationInstance.instance.startLocation(locationManager, minInterval, minDistance, locationListener)
            }
        }
    }

    fun stopLocation() {
        when (locationType) {
            CustomLocationType.LOCATION_GD -> {
                LogUtil.e(logger, "高德地图定位模式")
                GDLocationInstance.getInstance().stopLocation()
            }

            CustomLocationType.LOCATION_MULTIPLE -> {
                LogUtil.e(logger, "原生或华为鸿蒙定位为主 高德定位辅助定位")
                if (CommonUtil.isHarmonyOs()) {
                    HWLocationInstance.getInstance().stopLocation()
                } else {
                    GPSLocationInstance.instance.stopLocation(locationManager, locationListener)
                }
                GDLocationInstance.getInstance().stopLocation()
            }

            else -> { //[默认单一定位 安卓或华为地图定位服务]
                LogUtil.e(logger, "默认定位模式")
                if (CommonUtil.isHarmonyOs()) {
                    HWLocationInstance.getInstance().stopLocation()
                } else {
                    GPSLocationInstance.instance.stopLocation(locationManager, locationListener)
                }
            }
        }
    }


    companion object {

    }
}